package com.dataex.dbtool;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import sun.tools.tree.NewArrayExpression;

public class SqlAdapter {

	public static String getPrimaryKeySql() {
		return "";
	}

	public static String getTableIndexSql() {
		return "";
	}

	// CREATE INDEX index_name ON table_name (column_name)
	// 在表上创建一个唯一的索引。唯一的索引意味着两个行不能拥有相同的索引值。
	// CREATE UNIQUE INDEX index_name
	// ON table_name (column_name)
	// CREATE TABLE Persons
	// (
	// Id_P int NOT NULL,
	// LastName varchar(255) NOT NULL,
	// FirstName varchar(255),
	// Address varchar(255),
	// City varchar(255),
	// PRIMARY KEY (Id_P)
	// )

	// CREATE INDEX "dept_index" ON "public"."dept_map" USING btree (domain,
	// dept_id);
	// ALTER TABLE "public"."dept_map" ADD PRIMARY KEY ("domain", "dept_id");

	public static List<String> getSqlCreateStr(String tablename, Map<String, SqlFieldConfig> cfgMap, Map<String, SqlFieldConfig> defaultMap) {

		StringBuilder sb = new StringBuilder();
		StringBuilder sb2 = new StringBuilder();
		StringBuilder sb3 = new StringBuilder();
		StringBuilder sb2_2 = new StringBuilder();
		StringBuilder sb3_2 = new StringBuilder();

		sb.append("create table ").append(tablename).append("(");
		sb2.append("ALTER TABLE ").append("\"" + tablename + "\" ADD PRIMARY KEY (");
		sb3.append("CREATE INDEX " + "iname_" + System.currentTimeMillis()).append(" ON ").append(tablename);

		for (Map.Entry<String, SqlFieldConfig> en : cfgMap.entrySet()) {

			// field name
			SqlFieldConfig cg = en.getValue();
			if (cg.filedType == Long.class) {
				sb.append("'" + cg.fieldName + "'").append(" int8 ");
				sb.append(" not null,");
			} else if (cg.filedType == Integer.class) {
				sb.append("'" + cg.fieldName + "'").append(" int4 ");
				sb.append(" not null,");
			} else if (cg.filedType == String.class) {

				sb.append("'" + cg.fieldName + "'").append(" varchar(255) ");
				sb.append(" not null,");
			}

			// primary key
			if (en.getValue().primaryKey) {
				sb2_2.append("\"").append(en.getKey()).append("\",");
			}

			// index
			if (en.getValue().fieldIndex) {
				sb3_2.append("\"").append(en.getKey()).append("\",");
			}

		}
		sb.setCharAt(sb.length() - 1, ' ');
		sb.append(")");

		if (sb2_2.length() > 0) {
			sb2_2.setCharAt(sb.length() - 1, ' ');
			sb2_2.append(")");
		} else
			sb2 = new StringBuilder();
		if (sb3_2.length() > 0) {
			sb3_2.setCharAt(sb.length() - 1, ' ');
			sb3_2.append(")");
		} else
			sb3 = new StringBuilder();

		List<String> ret = new ArrayList<String>();
		ret.add(sb.toString());
		ret.add(sb2.toString());
		ret.add(sb3.toString());
		return ret;
	}

	public static String getSqlSelectStr(String tableName, Map<String, Object> dataMap) {
		// "insert into table app_org_active_num,app_type,analyse_type,app_active_num,app_active_rate,analyse_time) values(?,?,?,?,?,?)";
		// "update code_record set join_count=?, update_time=? where id=?";
		StringBuilder str = new StringBuilder();
		str.append("select * from ").append(tableName).append(" where ");

		StringBuilder str2 = new StringBuilder();
		for (Map.Entry<String, Object> en : dataMap.entrySet()) {
			if (str2.length() > 0)
				str2.append(" and ");
			str2.append(en.getKey()).append("='").append(en.getValue().toString()).append("'");
		}
		str.append(str2);
		return str.toString();
	}

	public static String getSqlDeleteStr(String tableName, Map<String, Object> dataMap, Set<String> keyCol) {
		// "insert into table app_org_active_num,app_type,analyse_type,app_active_num,app_active_rate,analyse_time) values(?,?,?,?,?,?)";
		// "update code_record set join_count=?, update_time=? where id=?";
		StringBuilder str = new StringBuilder();
		str.append("delete from ").append(tableName).append(" where ");

		StringBuilder str2 = new StringBuilder();
		for (String strK : keyCol) {
			if (str2.length() > 0)
				str2.append(" and ");
			str2.append(strK).append("=?");
		}

		str.append(str2);
		return str.toString();
	}

	public static String getSqlUpdateStr(String tableName, Map<String, Object> dataMap, Set<String> keyCol) {
		// "insert into table app_org_active_num,app_type,analyse_type,app_active_num,app_active_rate,analyse_time) values(?,?,?,?,?,?)";
		// "update code_record set join_count=?, update_time=? where id=?";
		StringBuilder str = new StringBuilder();
		str.append("update ").append(tableName).append(" set ");
		for (Map.Entry<String, Object> en : dataMap.entrySet()) {

			str.append(en.getKey()).append("=?,");
		}
		str.setCharAt(str.length() - 1, ' ');

		StringBuilder str2 = new StringBuilder();
		for (String strK : keyCol) {
			if (str2.length() > 0)
				str2.append(" and ");
			str2.append(strK).append("=?");
		}

		str.append(" where ").append(str2);
		return str.toString();
	}

	public static String getSqlInsertStr(String tableName, Map<String, Object> dataMap) {
		// "insert into table app_org_active_num,app_type,analyse_type,app_active_num,app_active_rate,analyse_time) values(?,?,?,?,?,?)";
		StringBuilder str = new StringBuilder();
		StringBuilder str2 = new StringBuilder();
		str.append("insert into ").append(tableName + " ");
		str.append("(");
		for (Map.Entry<String, Object> en : dataMap.entrySet()) {

			str.append(en.getKey() + ",");
			str2.append("?,");
		}

		str.setCharAt(str.length() - 1, ' ');
		str2.setCharAt(str2.length() - 1, ' ');
		str.append(") values (");
		str.append(str2);
		str.append(")");
		return str.toString();
	}

	public static String getSqlInsertStrWithValue(String tableName, Map<String, Object> dataMap) {
		// "insert into table app_org_active_num,app_type,analyse_type,app_active_num,app_active_rate,analyse_time) values(?,?,?,?,?,?)";
		StringBuilder str = new StringBuilder();
		StringBuilder str2 = new StringBuilder();
		str.append("insert into ").append(tableName + " ");
		str.append("(");
		for (Map.Entry<String, Object> en : dataMap.entrySet()) {

			str.append(en.getKey() + ",");
			Object dataO = SqlAdapter.getSqlFieldValueObject(  tableName, en.getKey(), en.getValue(), null);
			str2.append(getSqlFieldValue(dataO, 0)).append(",");
		}

		str.setCharAt(str.length() - 1, ' ');
		str2.setCharAt(str2.length() - 1, ' ');
		str.append(") values (");
		str.append(str2);
		str.append(")");
		return str.toString();
	}

	public static List<Map<String, Object>> getSqlMapDataList(List<?> dataList) {
		List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
		Map<String, Object> typeVal = null;
		for (Object obj : dataList) {
			typeVal = ImpBeanUtil.ThriftObjToMap(obj, true, null, null);
			mapList.add(typeVal);
		}
		return mapList;
	}

	public static Map<String, Object> getSqlMapData(Object obj) {

		Map<String, Object> typeVal = null;
		typeVal = ImpBeanUtil.ThriftObjToMap(obj, true, null, null);
		return typeVal;
	}

	public static Object correctData(Object item, SqlFieldConfig config, SqlFieldConfig defaultCfg) {
		if (item == null) {
			return "";
		}
		return item;
	}

	@SuppressWarnings("unused")
	private static String getSqlFieldValue(Object value, int getType) {
		if (value == null)
			return "null";
		Class<?> typeClass = value.getClass();
		if (typeClass == int.class || typeClass == Integer.class) {
			return value.toString();
		} else if (typeClass == short.class || typeClass == Short.class) {
			return value.toString();
		} else if (typeClass == byte.class) {
			return value.toString();
		} else if (typeClass == double.class || typeClass == Double.class) {
			return value.toString();
		} else if (typeClass == boolean.class || typeClass == Boolean.class) {
			return ((boolean) value) ? "1" : "0";
		} else if (typeClass == float.class || typeClass == Float.class) {
			return value.toString();
		} else if (typeClass == long.class || typeClass == Long.class) {
			return value.toString();
		} else if (typeClass == String.class || typeClass == java.util.Date.class || typeClass == java.sql.Timestamp.class) {
			return "'" + value.toString() + "'";
		}
		return "null";
	}

	public static Object getSqlFieldValueObject(String table, String key, Object value, SqlFieldConfig cfg) {
		if (value == null)
			return null;
		//String tableDDD = (tableName.equals("jzt_mchk") || tableName.equals("jzt_spkfk")) ? "ddd" : "";
		if (  key.equals("dmrq") && table.equals("jzt_spkfk")  ){
			String dateStr = value.toString();
			if ( dateStr.indexOf(':') > -1 ){
				dateStr = DateUtil.date2Str(DateUtil.str2Date(dateStr), "yyyy/MM/dd HH:mm:ss"); //2013/7/25  8:32:31 , "yyyy-MM-dd HH:mm:ss"
			}
			else if ( dateStr.indexOf('-') > -1 ){
				dateStr = DateUtil.date2Str(DateUtil.str2Date(dateStr, "yyyy-MM-dd"), "yyyy/MM/dd HH:mm:ss"); //2013/7/25  8:32:31 , "yyyy-MM-dd HH:mm:ss"
			}
			return dateStr;
		}
		else if ( key.equals("denglrq")  ){
			String dateStr = value.toString();
			if ( dateStr.indexOf(':') > -1 ){
				dateStr = DateUtil.date2Str(DateUtil.str2Date(dateStr), "yyyy/MM/dd"); //2013/7/25  8:32:31 , "yyyy-MM-dd HH:mm:ss"
			}
			else if ( dateStr.indexOf('-') > -1 ){
				dateStr = DateUtil.date2Str(DateUtil.str2Date(dateStr, "yyyy-MM-dd"), "yyyy/MM/dd"); //2013/7/25  8:32:31 , "yyyy-MM-dd HH:mm:ss"
			}
			return dateStr;
		} 
		else if ( key.equals("rq")){
			String dateStr = value.toString();
			if ( dateStr.indexOf(':') > -1 ){
				dateStr = DateUtil.date2Str(DateUtil.str2Date(dateStr), "yyyy/MM/dd HH:mm:ss"); //2013/7/25  8:32:31 , "yyyy-MM-dd HH:mm:ss"
			}
			else if ( dateStr.indexOf('-') > -1 ){
				dateStr = DateUtil.date2Str(DateUtil.str2Date(dateStr, "yyyy-MM-dd"), "yyyy/MM/dd HH:mm:ss"); //2013/7/25  8:32:31 , "yyyy-MM-dd HH:mm:ss"
			}
			return dateStr;	
		}
		else if ( key.equals("ontime") || key.equals("sxrq")||  key.equals("xkzyxq") || key.equals("yyzzyxq") || key.equals("dmrq")){
			String dateStr = value.toString();
			if ( dateStr.indexOf(':') > -1 ){
				dateStr = DateUtil.date2Str(DateUtil.str2Date(dateStr), "yyyy/MM/dd HH:mm:ss"); //2013/7/25  8:32:31 , "yyyy-MM-dd HH:mm:ss"
			}
			else if ( dateStr.indexOf('-') > -1 ){
				dateStr = DateUtil.date2Str(DateUtil.str2Date(dateStr, "yyyy-MM-dd"), "yyyy/MM/dd"); //2013/7/25  8:32:31 , "yyyy-MM-dd HH:mm:ss"
			}
			return dateStr;
		}
		else
			return value;
	}

}
